﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;

namespace HFController
{
    /// <summary>
    /// 委托业务逻辑
    /// </summary>
    public class DelegateController:ControllerBase
    {
        //单列
        private static DelegateController _delegateController;
        public static DelegateController getController()
        {
            if (_delegateController == null)
            {
                _delegateController = new DelegateController();
            }
            return _delegateController;
        }

        #region 案件委托

        /// <summary>
        /// 案件委托数据库访问
        /// </summary>
        private hf.DAL.CaseDelegate CaseDelegateDAL = new hf.DAL.CaseDelegate();

        /// <summary>
        /// 根据ID 得到一条案件委托列
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public hf.Model.CaseDelegate getCaseDelegateModel(int ID)
        {
            hf.Model.CaseDelegate model = new hf.Model.CaseDelegate();
            int total;

            List<hf.Model.CaseDelegate> list = CaseDelegateDAL.GetCaseDelegateList(1, 1, "ID=" + ID.ToString(), out total);

            if (list.Count > 0)
            {
                model = list[0];
            }
            else
            {
                model = null;
            }
            return model;
        }

        /// <summary>
        /// 得到委托列表（前台用）
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageNumber"></param>
        /// <param name="strWhere"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public List<hf.Model.CaseDelegate> getCaseDelegateList(int PageSize ,int PageNumber ,string strWhere ,out int total)
        {
            List<hf.Model.CaseDelegate> list = new List<hf.Model.CaseDelegate>();
            //获得列表
            list = CaseDelegateDAL.GetCaseDelegateList(PageSize, PageNumber, strWhere, out total);

            return list;
        }

        /// <summary>
        /// 得到案件委托列表
        /// </summary>
        /// <param name="PagerStr"></param>
        /// <returns></returns>
        public List<hf.Model.CaseDelegate> getCaseDelegateList(out string PagerStr)
        {
            List<hf.Model.CaseDelegate> list = new List<hf.Model.CaseDelegate>();

            //没有显示记录
            int PageSize = 10;
            //当前页码
            int PageNumber = 1;
            if (HttpContext.Current.Request.QueryString["Page"] != null)
            {
                PageNumber = int.Parse(HttpContext.Current.Request.QueryString["Page"].ToString());
            }
            //记录总条数
            int total;
            //筛选条件
            string strSql = "";

            //获得列表
            list = CaseDelegateDAL.GetCaseDelegateList(PageSize, PageNumber, strSql, out total);

            //分页
            PagerStr = LLL.Pager.TrunPageHtmlCode(PageSize, PageNumber, total, "");

            return list;
        }

        /// <summary>
        /// 绑定参数
        /// </summary>
        /// <param name="list"></param>
        private void BindParameter(List<hf.Model.CaseDelegate> list)
        {
            BagePages bp = new BagePages();
            foreach (hf.Model.CaseDelegate d in list)
            {
                d.CityName = bp.getCityName(d.CityID.ToString());
                d.ProvinceName = bp.getProvinceName(d.ProvinceID.ToString());
                d.TimeStr = bp.TimeDifference(DateTime.Now, d.Addtime);
            }
        }

        /// <summary>
        /// 发布案例
        /// </summary>
        /// <param name="endtimeCount"></param>
        /// <param name="email"></param>
        /// <param name="cityid"></param>
        /// <param name="provinceid"></param>
        /// <param name="phone"></param>
        /// <param name="delegatepersontype"></param>
        /// <param name="delegatename"></param>
        /// <param name="delegatemoney"></param>
        /// <param name="involvedcasemoney"></param>
        /// <param name="delegateclaim"></param>
        /// <param name="delegatecontent"></param>
        /// <param name="classid"></param>
        /// <returns></returns>
        public HFModel.ResultModel doAddDelegate(
                string endtimeCount ,
                string email ,
                string cityid ,
                string provinceid ,
                string phone ,
                string delegatepersontype ,
                string delegatename ,
                string delegatemoney ,
                string involvedcasemoney ,
                string delegateclaim ,
                string delegatecontent ,
                string classid
            )
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            hf.Model.CaseDelegate model = new hf.Model.CaseDelegate();
            BagePages bp = new BagePages();
            hf.Model.User user = bp.getLoginAccount();

            if (user != null)
            {
                model.UserID = user.ID;
                model.Status = 0;
                model.ReplyCount = 0;
                model.ProvinceID = int.Parse(provinceid);
                model.Phone = phone;
                model.LegalAffairsClassID = int.Parse(classid);
                hf.Model.LegalAffairsBiglClass lab = LegalAffairsController.getController().getLegalAffairsBiglClassModel(model.LegalAffairsClassID);
                model.LegalAffairsID = LegalAffairsController.getController().getLegalAffairsModel(lab.LegalAffairsID).ID;
                model.isCheck = 0;
                model.InvolvedCaseMoney = int.Parse(involvedcasemoney);
                model.Endtime = DateTime.Now.AddDays(double.Parse(endtimeCount));
                model.Email = email;
                model.DelegatePersonType = delegatepersontype;
                model.DelegateName = delegatename;
                model.DelegateMoney = int.Parse(delegatemoney);
                model.DelegateContent = delegatecontent;
                model.DelegateClaim = delegateclaim;
                model.CityID = int.Parse(cityid);
                model.Addtime = DateTime.Now;
                try {
                    CaseDelegateDAL.Add(model);
                    result.isTrue = true;
                    result.Message = "发布成功！您的案例通过审核后将发会显示在网站上";
                }
                catch (Exception err)
                {
                    result.isTrue = false;
                    result.Message = err.Message;
                }
            }
            else {
                result.isTrue = false;
                result.Message = "未登录";
            }

            return result;
        }

        /// <summary>
        /// 根据IP得到CaseDelegateList
        /// </summary>
        /// <param name="Count"></param>
        /// <param name="isLocation"></param>
        /// <returns></returns>
        public List<hf.Model.CaseDelegate> getCaseDelegateList(int Count ,bool isLocation)
        {
            List<hf.Model.CaseDelegate> list = new List<hf.Model.CaseDelegate>();
            //记录总条数
            int total;
            //筛选条件
            string strSql = "";
            if (isLocation)
            {
                //得到IP地址库

                hf.Model.IPAddress ipAddress = CityController.getController().ipAddress(HttpContext.Current.Request.ServerVariables["Remote_Addr"].ToString() == "::1" ? "127.0.0.1" : HttpContext.Current.Request.ServerVariables["Remote_Addr"].ToString());
                if (ipAddress != null)
                {
                    int ProvinceID = CityController.getController().getProvinceList().Find(i => i.ProvinceName == ipAddress.Province).ID;
                    int CityID = CityController.getController().getCityList().Find(i => i.CityName == ipAddress.City).ID;
                    strSql = "ProvinceID=" + ProvinceID.ToString() + " and CityID=" + CityID.ToString();
                }
                else {
                    strSql = "ProvinceID=1 and CityID=1";
                }
            }
            if (strSql == "")
            {
                strSql = "isCheck = 1";
            }
            else {
                strSql = strSql + " and isCheck = 1";
            }
            //获得列表
            list = CaseDelegateDAL.GetCaseDelegateList(Count, 1, strSql, out total);
            this.BindParameter(list);
            return list;
        }

        /// <summary>
        /// 删除案件委托
        /// </summary>
        /// <param name="ID"></param>
        public void doDelegeCaseDelegate(int ID)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();

            try
            {
                CaseDelegateDAL.Delete(ID);

                result.isTrue = true;
                result.Message = "删除成功！";
                result.goUrl = "location='CaseDelegateList.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="IDS"></param>
        public void doDelegeCaseDelegate(string[] IDS)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            try
            {
                for (int i = 0; i < IDS.Length; i++)
                {
                    CaseDelegateDAL.Delete(int.Parse(IDS[i].Trim()));
                }
                result.isTrue = true;
                result.Message = "删除成功！";
                result.goUrl = "location='CaseDelegateList.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }
        #endregion
    }
}
